// ANES24PilotEthnicity
log using ANES24PilotEthnicity.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_pilot_2024_dta_20240319.dta" // American National Election Study 2024 Pilot Study, March 19, 2024 Version. Date accessed: March 09, 2025.

keep if sample_type == 1 // Only keep those respondents with proper weight, as advised in codebook

//Create social justice scale
*Delete missing values
replace group_antifa= . if inlist(group_antifa, -7, 999)

*Generate a dummy variable for raceadvantage
gen raceadwhite=. 
replace raceadwhite=1 if raceadvt_white==3 & raceadvt_whitestr==1
replace raceadwhite=2 if raceadvt_white==3 & raceadvt_whitestr==2
replace raceadwhite=3 if raceadvt_white==3 & raceadvt_whitestr==3
replace raceadwhite=4 if raceadvt_white==2
replace raceadwhite=5 if raceadvt_white==1 & raceadvt_whitestr==3
replace raceadwhite=6 if raceadvt_white==1 & raceadvt_whitestr==2
replace raceadwhite=7 if raceadvt_white==1 & raceadvt_whitestr==1

*Reverse code certain items so social justice values are high
foreach var in police_number trans_health school_gender {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rschool_gender rtrans_health rpolice_number raceadwhite group_blm group_antifa {
    summarize `var'
    gen s_`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in s_rschool_gender s_rtrans_health s_rpolice_number s_raceadwhite s_group_blm s_group_antifa {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_score = 0
gen count_nonzero = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in s_rschool_gender s_rtrans_health s_rpolice_number s_raceadwhite s_group_blm s_group_antifa {
    replace total_score = total_score + `var'
    replace count_nonzero = count_nonzero + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = . 
replace SocJusValues = total_score / count_nonzero if count_nonzero > 0

//Demographic items
*Delete missing values and rename
replace faminc_new = . if inlist(faminc_new, 97, -7)
rename gender FemaleGender

*Create dummies
gen Graduate=. 
replace Graduate=0 if inrange(educ, 1, 4)
replace Graduate=1 if inlist(educ, 5, 6)

gen Black=.
replace Black=1 if race==2
replace Black=0 if race==1
replace Black=0 if inrange(race, 3, 8)

gen Hispanic=.
replace Hispanic=1 if race==3
replace Hispanic=0 if inlist(race, 1, 2)
replace Hispanic=0 if inrange(race, 4, 8)

gen White=.
replace White=1 if race==1
replace White=0 if inrange(race, 2, 8)

// Standardize 
egen Age = std(age)
egen Income = std(faminc_new)

// Regressions
regress SocJusValues Age FemaleGender Graduate Income White [pweight=weight], robust
eststo
regress SocJusValues Age FemaleGender Graduate Income Black [pweight=weight], robust
eststo
regress SocJusValues Age FemaleGender Graduate Income Hispanic [pweight=weight], robust
eststo
esttab
eststo clear

// Correlations 
pwcorr SocJusValues White Black Hispanic [aweight=weight], sig

log close